<?php
/**
 * Created by PhpStorm.
 * User: Lihong
 * Date: 2017/8/26
 * Time: 10:06
 */

namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use common\models\User;
class UserExtends extends ActiveRecord{

    public static function tableName(){
        return '{{%user_extends}}';
    }

    public function scenarios(){
        return [
            'create' => ['user_id'],
            'update' => ['user_id','nick_name', 'last_login', 'last_ip', 'photo_id', 'age', 'sex'],
        ];
    }

    public function rules(){
        return [
            ['user_id', 'exist', 'targetClass' => 'common\models\User', 'targetAttribute' => ['user_id'=> 'id']],
            ['nick_name', 'string', 'min' => 1, 'max' => 20],
            ['nick_name','required'],
            ['last_login', 'default', 'value' => time()],
            ['last_ip', 'string', 'min' => 7, 'max' => 16],
            ['photo_id', 'exist', 'targetClass' => 'backend\models\Files', 'targetAttribute' => ['photo_id' => 'id']],
            ['photo_id', 'default', 'value' => 1],
            ['age', 'integer'],
            ['age', 'default', 'value' => 18],
            ['sex', 'in', 'range' => [0,1,2]],
            ['sex', 'default', 'value' => 0],
        ];
    }

    public function attributeLabels(){
        return [
            'user_id' => '用户ID',
            'nick_name' => '用户昵称',
            'last_login' => '登陆时间',
            'last_ip' => '登陆IP',
            'photo_id' => '头像ID',
            'age' => '年龄',
            'sex' => '性别',
            'description' => '用户简介',
        ];
    }

    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
}